package com.hyperchain.config.mq.work;


import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class HelloSender {

    @Resource
    private AmqpTemplate rabbitTemplate;

    public void send(String message) {
        // 创建消息属性
        MessageProperties messageProperties = new MessageProperties();

        // 生成唯一的消息 ID
        String messageId = UUID.randomUUID().toString();
        messageProperties.setMessageId(messageId);  // 设置唯一的消息 ID
        // 创建消息对象
        Message messages = new Message(message.getBytes(), messageProperties);
        //简单对列的情况下routingKey即为Q名
        rabbitTemplate.convertAndSend("q_hello", messages);
        System.out.println("发送了一条消息 : " + messages);
    }
}


